//
//  RGLoginViewController.m
//  RedGrass
//
//  Created by wukong on 2018/11/23.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGLoginViewController.h"
#import "RGLoginView.h"
#import "RGLoginViewModel.h"

@interface RGLoginViewController()<RGLoginViewDelegate>

@property (strong, nonatomic) RGLoginView *loginView;
@property (strong, nonatomic) RGLoginViewModel *viewModel;

@end

@implementation RGLoginViewController


-(void)viewDidLoad{
    [super viewDidLoad];
    self.NavStyleType = RGCustomNavStyleType_default;
    [self showBackButtonWithImage:@"关闭" target:self selector:@selector(dismissVC)];
    [self showRightButtonWithTitle:@"密码登录" target:self selector:@selector(passwordLogin)];
    [self.customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(34);
        }else{
            make.bottom.mas_equalTo(0);
        }
    }];
    [self.customContentView addSubview:self.loginView];
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}


-(void)dismissVC{
    KPostNotification(KNotificationLoginStateChange, @YES);
}

-(void)passwordLogin{
   [RGLoginRouter pushToLoginWithPassViewControllerFromVC:self];
}


-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

#pragma mark - viewDelegate
-(void) gainCaptchaCodeWithLoginView:(RGLoginView *)view{
    
    if(view.phoneNumber.length == 0){
       [self showTextMessageWithMessage:@"请输入手机号码"];
        return;
    }else{
       [self.viewModel gainCaptchaCodeWithPhone:view.phoneNumber];
//        [view begainGainCodeTimer];
    }
}

//wechat登录
-(void) weChatloginWithLoginView:(RGLoginView *)view{
    
    AA_WEAK_MAKE(self) weakSelf = self;
    [[XLMShareSdkTool shareInstance] setLoginBlock:^(BOOL success, NSDictionary *dic) {
        NSString *uid = [dic objectForKey:@"code"];
//        NSDictionary *dic1 = @{@"platform":@"wechat",
//                               @"code":SafeStr(uid),
//                               };
        [weakSelf otherLoginWithParams:SafeStr(uid)];
    }];
    [XLMShareSdkTool thirdLoginWithWeChatResult];
}

//第三方登录
-(void) otherLoginWithParams:(NSString *)code{
    [self.viewModel loginWithWechatWithCode:code];

}


//登录遇到问题
-(void) loginFaiureWithLoginView:(RGLoginView *)view{
    
}

//登陆
-(void) passwordLoginWithLoginView:(RGLoginView *)view{
    if(view.phoneNumber.length == 0){
        [self showTextMessageWithMessage:@"请输入手机号码"];
        return;
    }else if(view.captchaCode.length == 0){
        [self showTextMessageWithMessage:@"请输入验证码"];
        return;
    }else{
        [self.viewModel loginWithCaptchaCode:view.captchaCode phone:view.phoneNumber];
    }
}

#pragma mark - protocol
//开始倒计时
-(void) ViewBegainGainCodeTimer{
    [_loginView begainGainCodeTimer];
}

//验证码登陆
-(void) loginWithCaptchaCodeSuccess{
//    [self showTextMessageWithMessage:@"功能还在开发中"];
     [[AppDelegate shareAppDelegate] reInitTabarViewController];
     KPostNotification(KNotificationLoginStateChange, @YES);
}


#pragma mark - getter

-(RGLoginView *)loginView{
    if(!_loginView){
        _loginView = [[RGLoginView alloc]init];
        _loginView.viewDelegate = self;
    }
    return _loginView;
}

-(RGLoginViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[RGLoginViewModel alloc]initWithProtocol:self];
    }
    return _viewModel;
}

@end
